import moment from 'moment'
const date = {
	//转换时间
	transformation(data) {
		let result = ''
		var minute = 1000 * 60;
		var hour = minute * 60;
		var day = hour * 24;
		var halfamonth = day * 15;
		var month = day * 30;
		var year = month * 12
		let timeStamp = new Date().getTime()
		var diffValue = timeStamp - data * 1000;
		if (diffValue < 0) {
			return;
		}
		var monthC = diffValue / month;
		var weekC = diffValue / (7 * day);
		var dayC = diffValue / day;
		var hourC = diffValue / hour;
		var yearC = diffValue / year;
		if (yearC >= 1) {
			result = "" + parseInt(yearC) + "年前";
		} else if (monthC >= 1) {
			result = "" + parseInt(monthC) + "月前";
		} else if (weekC >= 1) {
			result = "" + parseInt(weekC) + "周前";
		} else if (dayC >= 1) {
			result = "" + parseInt(dayC) + "天前";
		} else if (hourC >= 1) {
			result = "" + parseInt(hourC) + "小时前";
		} else {
			result = "刚刚";
		}
		return result;
	},
	phoneNumFilter(phone) {
		if(phone == null || typeof phone === 'undefined') return ''
		// 1字符串转化成数组
		let phoneArr = [...phone];
		let str = ''
		// 2.将数组中的4-7位变成*
		phoneArr.map((res, index) => {
			if (index > 2 && index < 7) {
				str += '*';
				// return '*';
			} else {
				str += res;
				// return res;
			}
		});
		return str;
	},
	idCardFilter(str) {
		if(!str) return ''
		var card = str.replace(/^(.{4})(?:\d+)(.{4})$/, '$1**********$2')
		return card
	},
	cardFilter(num) {
			return '****  ****  ****  ' + num.substring(num.length - 4);
	},
	num2chinese(money) {
		var cnNums = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); //汉字的数字
		var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
		var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
		var cnDecUnits = new Array("角", "分", "毫", "厘"); //对应小数部分单位
		var cnInteger = "整"; //整数金额时后面跟的字符
		var cnIntLast = "元"; //整型完以后的单位
		var maxNum = 999999999999999.9999; //最大处理的数字
		var IntegerNum; //金额整数部分
		var DecimalNum; //金额小数部分
		var ChineseStr = ""; //输出的中文金额字符串
		var parts; //分离金额后用的数组，预定义
		var Symbol = "";//正负值标记
		if (money == "") {
			return "";
		}
			
		money = parseFloat(money);
		if (money >= maxNum) {
			alert('超出最大处理数字');
			return "";
		}
		if (money == 0) {
			ChineseStr = cnNums[0] + cnIntLast + cnInteger;
			return ChineseStr;
		}
		if (money < 0) {
			money = -money;
			Symbol = "负 ";
		}
		money = money.toString(); //转换为字符串
		if (money.indexOf(".") == -1) {
			IntegerNum = money;
			DecimalNum = '';
		} else {
			parts = money.split(".");
			IntegerNum = parts[0];
			DecimalNum = parts[1].substr(0, 4);
		}
		    if (parseInt(IntegerNum, 10) > 0) { //获取整型部分转换
		        var zeroCount = 0;
		        var IntLen = IntegerNum.length;
		        for (var i = 0; i < IntLen; i++) {
		            var n = IntegerNum.substr(i, 1);
		            var p = IntLen - i - 1;
		            var q = p / 4;
		            var m = p % 4;
		            if (n == "0") {
		                zeroCount++;
		            }
		            else {
		                if (zeroCount > 0) {
		                    ChineseStr += cnNums[0];
		                }
		                zeroCount = 0; //归零
		                ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
		            }
		            if (m == 0 && zeroCount < 4) {
		                ChineseStr += cnIntUnits[q];
		            }
		        }
		        ChineseStr += cnIntLast;
		        //整型部分处理完毕
		    }
		    if (DecimalNum != '') { //小数部分
		        var decLen = DecimalNum.length;
		        for (var i = 0; i < decLen; i++) {
		            var n = DecimalNum.substr(i, 1);
		            if (n != '0') {
		                ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
		            }
		        }
		    }
		    if (ChineseStr == '') {
		        ChineseStr += cnNums[0] + cnIntLast + cnInteger;
		    } else if (DecimalNum == '') {
		        ChineseStr += cnInteger;
		    }
		ChineseStr = Symbol + ChineseStr;
			
		return ChineseStr;
	}
}
export default date
